import listnode.ListNode;

public class _0313_移除链表元素 {

    public ListNode removeElements(ListNode head, int val) {
        if (head == null) {
            return null;
        }
        head.next = removeElements(head.next, val);
        if (head.val == val){
            return head.next;
        }else {
            return head;
        }
    }

    public ListNode removeElements1(ListNode head, int val) {
        while(head!=null && head.val == val){
            head = head.next;
        }
        if(head == null){
            return null;
        }
        ListNode pre = head;
        while (pre.next != null) {
            if (pre.next.val == val) {
                pre.next = pre.next.next;
            }else {
                pre = pre.next;
            }
        }
        return head;
    }

    public ListNode removeElements2(ListNode head, int val) {
        if (head == null) {
            return null;
        }
        ListNode vmNode = new ListNode(val-1);
        vmNode.next = head;
        ListNode pre = vmNode;
        while (pre.next!=null){
            if (pre.next.val == val) {
                pre.next = pre.next.next;
            }else {
                pre = pre.next;
            }
        }
        return vmNode.next;
    }


    public static void main(String[] args) {

    }

}
